package com.yun.controller;

import com.yun.utils.DownloadPdf;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
public class IndexController {
    @PostMapping ("login")

    public String index() {
        System.err.println("开始登录");
        return "login";
    }


    @GetMapping("findAll")
    @PreAuthorize("hasAnyAuthority('menu:system')")
    public String findAll() {
        return "findAll";
    }




    @GetMapping("test1")

    public String test1() {
        return "test1";
    }



    @GetMapping("find")

    public String find() {
        return "find";
    }


    @GetMapping("/success")

    public String success(){
        return "success"; }


    @GetMapping("/fail")

    public String fail() {
        return "fail";
    }

/*    @GetMapping("/test/index")
    public String index1() {
        return "成功了";
    }*/
    //unauth  /testrole

    @GetMapping("/testrole")

    public String testrole() {
        return "testrole";
    }



    @RequestMapping(value = "/download",method = RequestMethod.GET)
    public String downloadAbilityTemplate(String fileName, HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding("utf-8");//设置编码
        response.setContentType("multipart/form-data");//设置类型           response.setContentType("application/vnd.ms-excel");  excel下载


        response.setHeader("Content-Disposition", "attachment;fileName="+ "aa.pdf");                                       //设置响应头

        //response.addHeader("Content-Length", "" + file.length()); //file.length() 就是pdf的大小
        try {

            BufferedInputStream bufferedInputStream = DownloadPdf.downLoadByUrl("https://zhidao.baidu.com/question/1754091879046279468.html",
                    "test.pdf", "c:/test");

           response.addHeader("Content-Length", 10000+"");
          //  InputStream inputStream = new FileInputStream(new File("C:\\test\\ELISA.docx"));
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = bufferedInputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }//边读模板文件边写入输出流
            os.close();
            bufferedInputStream.close();//关流
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;                //注意此时return null
    }

}